package com.lz.timer.an.ddjobs.simple;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.dangdang.ddframe.job.api.ShardingContext;
import com.dangdang.ddframe.job.api.simple.SimpleJob;
import com.lz.common.util.DateUtil;
import com.lz.timer.an.entity.DspCampaign;
import com.lz.timer.an.entity.DspPutUnit;
import com.lz.timer.an.enums.CampaignStatusEnum;
import com.lz.timer.an.enums.DspPutUnitStatusEnum;
import com.lz.timer.an.service.DspCampaignService;
import com.lz.timer.an.service.DspPutUnitService;

import lombok.extern.slf4j.Slf4j;

/**
 * 推广计划投放中
 *
 * @author rench
 */
@Slf4j
public class StatisticalPlanInOperationJob implements SimpleJob {
	
	@Autowired
	private DspCampaignService dspCampaignService;
	
	@Autowired
	private DspPutUnitService dspPutUnitService;
	
    @Override
    public void execute(final ShardingContext context) {

        long threadId = Thread.currentThread().getId();

        int result = 0;
        // 当前分片
        int item = context.getShardingItem();      

        log.info("SIMPLE Thread:{}, ShardingContext:{}", threadId, context.toString());
        switch (item) {
        case 0:
             
        	log.info("==========================begin=============================");
        	log.info("++++++++++++++++推广计划投放中状态 到达投放结束时间+++++++++++++++++");
        	
            Map<String, Object> queryMap = new HashMap<String, Object>();
            String dateStr = DateUtil.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss");
            
            queryMap.put("plan_opera_time", dateStr);
            queryMap.put("eq_status", CampaignStatusEnum.PUTING.getValue());
            List<DspCampaign> dspCampaignList = dspCampaignService.selectListByMap(queryMap);
            log.info("thread:{} 符合条件的推广计划dspCampaignList:{}", threadId, dspCampaignList);
            
        	
            for (DspCampaign dspCampaign : dspCampaignList) {
            	dspCampaign.setStatus(CampaignStatusEnum.FINISHED.getValue());
            	
            	log.info("thread:{} 更新推广计划状态为 已完成 dspCampaign:{}", threadId, dspCampaign);
            	dspCampaignService.update(dspCampaign);
            	
            	queryMap.clear();
            	queryMap.put("campaign_no", dspCampaign.getCampaign_no());
            	List<DspPutUnit> dspPutUnitList = dspPutUnitService.selectListByMap(queryMap);
            	
            	log.info("thread:{} 推广计划下的投放单元 dspPutUnitList:{}", threadId, dspPutUnitList);
            	for (DspPutUnit dspPutUnit : dspPutUnitList) {
            		
            		log.info("thread:{} 修改推广计划下单元状态为 已完成", threadId);
            		dspPutUnit.setStatus(DspPutUnitStatusEnum.FINISHED.getValue());
            		dspPutUnitService.update(dspPutUnit);
				}
			}
        	
            log.info("====================+======end=================+============");
             
            break;
        default:
            log.warn("not total job, item:{}, Thread:{}", item, threadId);
            return;
        }

        log.info("job ：time = {} SIMPLE Thread:{}, result:{}, end", new Date(), threadId, result);

    }

}